import datetime
import os
import sys
import threading
import time

sys.path.append(os.path.dirname(os.path.split(os.getcwd())[0] + r"\ "))
import Global_param as g
from UserStruct import *


# 策略1
class strategy1(object):

    def __init__(self):
        # 订阅的合约
        self.subID = ["FG209", "SA209", 'au2206', 'sc2206']
        self.specific_strategy_map = {}
        for instrumentID in self.subID:
            self.specific_strategy_map[instrumentID] = strategy1.specific_strategy(instrumentID)
        # 策略编号
        self.strategyID = 1

    class specific_strategy(object):
        def __init__(self, instrumentID):
            # 策略数据
            # self.market_data = MarketData()
            self.market_data = None
            # 策略数据锁
            self.market_data_lock = threading.Lock()
            # 策略合约名称
            self.instrumentID = None

        def onQuote(self):
            # self.market_data_lock.acquire()

            # print('''策略{}\n订阅合约名称：{}\n最新价格：{}\n最后修改时间：{}\n最后修改毫秒：{}\n现在时间：{}\n
            #         '''.format(self.strategyID, self.market_data.InstrumentID, self.market_data.LastPrice,
            #                    self.market_data.UpdateTime, self.market_data.UpdateMillisec,
            #                    datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")))
            # print('策略{}, 订阅合约名称：{}'.format(self.strategyID, self.market_data.InstrumentID))
            # if self.market_data.InstrumentID == 'm2209':
            #     print(self.market_data.LastPrice)

            # 测试传送至策略模块花费时间
            # if self.market_data.InstrumentID == 'FG209':
            #     g.end = time.time()
            #     print(f'传送至策略模块花费时间：{g.end - g.start}s')
            self.market_data_lock.release()



